package com.virus.server.http;

import javax.servlet.http.Cookie;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Response extends BaseResponse {

    private OutputStream outputStream;

    private int status;

    private String responseLine;

    private String responseHead = "";

    private byte[] responseBody;

    private byte[] headerLine;

    private StringWriter stringWriter;

    private PrintWriter printWriter;

    private Map<String, String> responseHeaderMap = new HashMap<>();

    public Response(Socket socket) {
        this.stringWriter = new StringWriter();
        this.printWriter = new PrintWriter(stringWriter, true);
        try {
            this.outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.status = 200;
    }

    public PrintWriter getWriter() {
        return printWriter;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setHeader(String key, String value) {
        responseHeaderMap.put(key, value);
    }

    public String getHeader(String key) {
        return responseHeaderMap.get(key);
    }

    public void getResponseBody() {
        if (responseBody == null) {
            try {
                responseBody = this.stringWriter.toString().getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setResponseBody(byte[] bytes) {
        this.responseBody = bytes;
    }

    public void setContentType(String contentType) {
        responseHeaderMap.put("Content-Type", contentType);
    }

    public void  respond() {
        responseLine = "HTTP/1.1 " + status + System.lineSeparator();
        Set<String> headers = responseHeaderMap.keySet();
        StringBuffer sb = new StringBuffer();
        for (String key : headers) {
            String value = responseHeaderMap.get(key);
            sb.append(key + ":" + value + System.lineSeparator());
        }
        responseHead = sb.toString();
        getResponseBody();
        try {
            headerLine = (responseLine + responseHead + System.lineSeparator()).getBytes("utf-8");
            byte[] responseData = new byte[responseBody.length + headerLine.length];
            System.arraycopy(headerLine, 0, responseData, 0, headerLine.length);
            System.arraycopy(responseBody, 0, responseData, headerLine.length, responseBody.length);
            outputStream.write(responseData);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addCookie(Cookie cookie) {
        String name = cookie.getName();
        String value = cookie.getValue();
        String s = name + "=" + value;
        String header = getHeader("Set-Cookie");
        if (header == null){
            setHeader("Set-Cookie", s);
            return;
        }
        setHeader("Set-Cookie", header + ";" + s);
    }
}
